/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import abstractmodel.StatisticsType;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;

/**
 * 这个类的主要功能是存储统计的有关信息，并且提供了根据这些信息生成饼图和折线图的函数。
 * @author fightfxj
 */
public class StatisticsInfo {

    /**
     * names：当统计帐户的信息时为帐户明细的时间，当为统计账簿、用户的信息时为账簿帐户中帐户、账簿的名字
     * values：存放明细的余额值或者账簿、帐户的剩余额。
     * type：存放统计的类型  
     */
    private ArrayList<String> names;
    private ArrayList<Double> values;
    private StatisticsType type;

    public StatisticsInfo() {
        names = new ArrayList<String>();
        values = new ArrayList<Double>();
    }

    public StatisticsInfo(StatisticsType type) {
        names = new ArrayList<String>();
        values = new ArrayList<Double>();
        this.type = type;
    }

    public StatisticsType getType() {
        return type;
    }

    public void setType(StatisticsType type) {
        this.type = type;
    }

    public void add(String name, Double value) {
        names.add(name);
        values.add(value);
    }

    public ArrayList<String> getNames() {
        return names;
    }

    public ArrayList<Double> getValues() {
        return values;
    }

    private PieDataset createPieDataset() {
        DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
        for (int i = 0; i < names.size(); i++) {
            defaultpiedataset.setValue(names.get(i), values.get(i));
        }
        return defaultpiedataset;
    }

    private JFreeChart createPieChart(PieDataset piedataset) {
        JFreeChart jfreechart = ChartFactory.createPieChart("饼图", piedataset, true, true, true);
        TextTitle texttitle = jfreechart.getTitle();
        texttitle.setToolTipText("饼图");
        PiePlot pieplot = (PiePlot) jfreechart.getPlot();
        pieplot.setLabelFont(new Font("SansSerif", 0, 12));
        pieplot.setNoDataMessage("No data available");
        pieplot.setCircular(false);
        pieplot.setLabelGap(0.02D);
        return jfreechart;
    }

    /**
     * 
     * @return 绘有饼图的JPanel
     */
    public JPanel getPieChart() {
        if (this.type != abstractmodel.StatisticsType.PIE) {
            return null;
        }
        JFreeChart piechart = createPieChart(createPieDataset());
        return new ChartPanel(piechart);
    }

    private CategoryDataset createLineDataset() {
        DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
        for (int i = 0; i < names.size(); i++) {
            defaultcategorydataset.setValue(values.get(i).doubleValue(), "折线图", names.get(i));
        }
        return defaultcategorydataset;
    }

    private JFreeChart createLineChart(CategoryDataset categorydataset) {
        JFreeChart jfreechart = ChartFactory.createLineChart("折线图", "时间", "数额", categorydataset, PlotOrientation.VERTICAL, false, true, false);
        jfreechart.setBackgroundPaint(Color.white);
        CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
        categoryplot.setBackgroundPaint(Color.lightGray);
        categoryplot.setRangeGridlinesVisible(false);

        NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
        numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot.getRenderer();
        lineandshaperenderer.setShapesVisible(true);
        lineandshaperenderer.setDrawOutlines(true);
        lineandshaperenderer.setUseFillPaint(true);
        lineandshaperenderer.setBaseFillPaint(Color.white);
        lineandshaperenderer.setSeriesStroke(0, new BasicStroke(3F));
        lineandshaperenderer.setSeriesOutlineStroke(0, new BasicStroke(2.0F));
        lineandshaperenderer.setSeriesShape(0, new java.awt.geom.Ellipse2D.Double(-5D, -5D, 10D, 10D));
        return jfreechart;
    }

    /**
     * 
     * @return 绘有折线图的JPanel
     */
    public JPanel getLineChart() {
        if (this.type != abstractmodel.StatisticsType.LINE) {
            return null;
        }
        JFreeChart linechart = createLineChart(createLineDataset());
        return new ChartPanel(linechart);
    }

    private CategoryDataset createHistogramDataset() {
        DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
        for (int i = 0; i < this.names.size(); i++) {
            defaultcategorydataset.addValue(this.values.get(i).doubleValue(), this.names.get(i), "");
        }
        return defaultcategorydataset;
    }

    private JFreeChart createHistogramChart(CategoryDataset categorydataset) {
        JFreeChart jfreechart = ChartFactory.createBarChart("柱状图", "分布", "数额", categorydataset, PlotOrientation.VERTICAL, true, true, true);
        jfreechart.setBackgroundPaint(Color.white);
        CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
        categoryplot.setBackgroundPaint(Color.lightGray);
        categoryplot.setDomainGridlinePaint(Color.white);
        categoryplot.setDomainGridlinesVisible(true);
        categoryplot.setRangeGridlinePaint(Color.white);
        NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
        numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        CategoryAxis categoryaxis = categoryplot.getDomainAxis();
        categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(0.52359877559829882D));
        return jfreechart;
    }

    public JPanel getHistogramChart() {
        if (this.type != abstractmodel.StatisticsType.HISTOGRAM) {
            return null;
        }
        return new ChartPanel(createHistogramChart(createHistogramDataset()));
    }

    public JPanel getChart(abstractmodel.StatisticsType type) {
        switch (type) {
            case LINE:
                return getLineChart();
            case HISTOGRAM:
                return getHistogramChart();
            case PIE:
                return getPieChart();
            default:
                return null;
        }
    }
}
